package com.server.server_a10.mapper;

import com.server.server_a10.entity.Inf.HeaterIdInf;
import com.server.server_a10.entity.Inf.IdInf;
import com.server.server_a10.entity.heatingSystem.HeatingEquipment;
import com.server.server_a10.management.entity.EquipmentInf;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.List;

@Mapper
public interface HeaterMapper {
    @SelectProvider(type = HeaterMapper.ProviderHeater.class, method = "getHeaterByRoomId")
    List<HeatingEquipment> getHeaterByRoomId(@Param("room_id") int room_id);

    @SelectProvider(type = HeaterMapper.ProviderHeater.class, method = "getAllHeaterIdByHomeIdOrRoomId")
    List<HeaterIdInf> getAllHeaterIdByHomeIdOrRoomId(@Param("id") int id, @Param("type") String type);

    @SelectProvider(type = ProviderHeater.class,method = "getHeaterLimit")
    List<EquipmentInf> getHeaterLimit(String name, String room_id, String home_id, int index, int limit);

    @SelectProvider(type = ProviderHeater.class,method = "getHeaterLimitCount")
    int getHeaterLimitCount(String name,String room_id,String home_id);

    class ProviderHeater {
        public static String getHeaterByRoomId(int room_id) {
            return "select * from heating_equipment where room_id=#{room_id}";
        }

        /*
        获取房子/房间内的所有homeId、roomId、lightId
        入参：id为home_id或room_id
             type为"home_id"或"room_id"
          */
        public static String getAllHeaterIdByHomeIdOrRoomId(int id, String type) {
            return "select h.id as homeId, r.id as roomId, heating_equipment.id as heaterId\n" +
                    "from heating_equipment\n" +
                    "         left join room r on r.id = heating_equipment.room_id\n" +
                    "         left join home h on r.home_id = h.id\n" +
                    "where ${type} = #{id}";
        }

        public static String getHeaterLimit(){
            return "select heater.id as id,heater.name as name,heater.room_id as room_id,r.home_id as home_id,u.id as user_id,u.name as user_name\n" +
                    "from heating_equipment heater\n" +
                    "         left join room r on r.id = heater.room_id\n" +
                    "         left join home h on r.home_id = h.id\n" +
                    "         left join user u on h.master_id = u.id\n" +
                    "where heater.id like concat('%',#{name},'%') and  heater.room_id like concat('%',#{room_id},'%') and r.home_id like concat('%',#{home_id},'%')\n" +
                    "limit #{index},#{limit}";
        }

        public static String getHeaterLimitCount(){
            return "select count(*)\n" +
                    "from heating_equipment heater\n" +
                    "         left join room r on r.id = heater.room_id\n" +
                    "         left join home h on r.home_id = h.id\n" +
                    "         left join user u on h.master_id = u.id\n" +
                    "where heater.id like concat('%',#{name},'%') and  heater.room_id like concat('%',#{room_id},'%') and r.home_id like concat('%',#{home_id},'%')";
        }
    }
}